package com.example.planstudio;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.*;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xui.widget.guidview.GuideCaseView;
import es.dmoral.toasty.Toasty;
import org.jetbrains.annotations.NotNull;

import java.util.Calendar;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private FloatingActionButton add;
    private MyDBhelper myDBhelper;
    private MyAdapter myAdapter;
    private List<Note> resulList;
    private TextView text;
    private ImageView crown;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Calendar calendar = Calendar.getInstance();
        listView = findViewById(R.id.listview);
        add = findViewById(R.id.fab);
        text = findViewById(R.id.text1);
        text.setText(Calendar.MONTH + "月" + Calendar.DAY_OF_MONTH + "日");
        crown = findViewById(R.id.crown);
        crown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, MoneyActivity.class);
                startActivity(intent);
            }
        });
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, RecordActivity.class);
                startActivityForResult(intent, 1);
            }
        });

        init();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Note note = (Note) myAdapter.getItem(i);
                Intent intent = new Intent(MainActivity.this, RecordActivity.class);

                String sendId = note.getId();
                String sendEvent = note.getEvent();
                String sendTime = note.getTime();
                intent.putExtra("id", sendId);
                intent.putExtra("Event", sendEvent);
                intent.putExtra("Time", sendTime);
                startActivityForResult(intent, 1);
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {

                MaterialDialog.Builder builder = new MaterialDialog.Builder(MainActivity.this);
                builder.title("删除记录")
                        .content("你确定要删除这条记录吗？")
                        .positiveText("确定")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull @NotNull MaterialDialog dialog, @NonNull @NotNull DialogAction which) {
                                Note note = (Note) myAdapter.getItem(i);
                                String deleteId = note.getId();
                                if (myDBhelper.deleteData(deleteId)) {
                                    init();
                                    Toasty.success(MainActivity.this, "删除成功！！", Toast.LENGTH_LONG).show();
                                } else {
                                    Toasty.error(MainActivity.this, "删除失败！！", Toast.LENGTH_LONG).show();
                                }
                            }
                        })
                        .negativeText("取消")
                        .show();

                return true;
            }
        });

        new GuideCaseView.Builder(MainActivity.this)
                .focusOn(findViewById(R.id.crown))
                .focusCircleRadiusFactor(1.5)
                .title("这里添加计划")
                .focusBorderColor(Color.GREEN)
                .titleStyle(0, Gravity.BOTTOM | Gravity.CENTER)
                .fitWindowsAuto()
                .build()
                .show();

        new GuideCaseView.Builder(MainActivity.this)
                .focusOn(findViewById(R.id.fab))
                .focusCircleRadiusFactor(1.5)
                .title("这里添加计划")
                .focusBorderColor(Color.GREEN)
                .titleStyle(0, Gravity.BOTTOM | Gravity.CENTER)
                .fitWindowsAuto()
                .build()
                .show();

//        new GuideCaseView.Builder(MainActivity.this)
//                .title("日历可以下拉展开哦")
//                .focusRectAtPosition(550, 360, 800, 140)
//                .roundRectRadius(60)
//                .build()
//                .show();

    }

    private void init() {
        if (resulList != null) {
            resulList.clear();
        }
        myDBhelper = new MyDBhelper(MainActivity.this, "note.db", null, 1);
        resulList = myDBhelper.query();
        myAdapter = new MyAdapter(resulList, MainActivity.this);
        listView.setAdapter(myAdapter);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 2) {
            init();
        }
    }
}